package com.qf.voteconsumer.vote.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.qf.voteconsumer.vote.entity.Candidate;
import com.qf.voteconsumer.vote.entity.Vote;
import com.qf.voteconsumer.vote.mapper.CandidateMapper;
import com.qf.voteconsumer.vote.mapper.VoteMapper;
import com.qf.voteconsumer.vote.service.IVoteService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dong_
 * @since 2023-06-28
 */
@Service
@Slf4j
public class VoteServiceImpl extends ServiceImpl<VoteMapper, Vote> implements IVoteService {

    @Autowired
    ObjectMapper objectMapper;

    @Autowired
    CandidateMapper candidateMapper;

    @Transactional(rollbackFor = Exception.class) // 控制数据库事务
    @Override
    public void processMsg(String json) throws Exception {

        //1. json ---> hashap
        HashMap hashMap = objectMapper.readValue(json, HashMap.class);
        String votetime = (String) hashMap.get("votetime");
        // votetime ---> Date 对象
        // 2023-06-28T03:13:01.366+0000
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
        Date voteDate = simpleDateFormat.parse(votetime);
        String candidateName = (String) hashMap.get("candidateName");
        String phone = (String) hashMap.get("phone");

        //2. 更新candiate表中的投票信息
        // 根据候选人名字查询候选人信息
        Candidate candidate = candidateMapper.selectOne(new QueryWrapper<Candidate>().eq("name", candidateName));
        log.info("查询到候选人的得票信息为{}",candidate);
        Integer voteNum = candidate.getVoteNum();
        candidate.setVoteNum(voteNum+1);
        candidateMapper.updateById(candidate);

        //3. 把投票流水信息写入vote表中
        Integer candidateId = candidate.getId();
        // 根据手机号和候选人id查询 vote表
        Vote vote = this.getOne(new QueryWrapper<Vote>().eq("phone", phone).eq("candidate_id", candidateId));
        if (vote == null){
            log.info("该手机号{}第一次为候选人{}投票",phone,candidateName);
            // 如果没有，则说明该手机号是第一次给这个候选人投票，走insert
            vote = new Vote();
            vote.setCandidateId(candidateId);
            vote.setPhone(phone);
            vote.setVoteNum(1);
            vote.setUpdateTime(voteDate);
            this.save(vote);
        }else {
            log.info("该手机号{}再次次为候选人{}投票,共投了{}票",phone,candidateName,vote.getVoteNum());
            // 如果有数据，说明该手机号给这个候选人曾经透过票，update
            vote.setUpdateTime(voteDate);
            vote.setVoteNum(vote.getVoteNum()+1);
            this.updateById(vote);
        }

    }
}
